package cn.zwx.member.svc.service.impl;

import cn.zwx.common.mybatis.query.LambdaQueryWrapperX;
import cn.zwx.member.svc.domain.bo.CheckUserAddressExistBO;
import cn.zwx.member.svc.domain.constans.MemberConstans;
import cn.zwx.member.svc.domain.entity.AddressEntity;
import cn.zwx.member.svc.domain.enums.AddressTyepEnum;
import cn.zwx.member.svc.mapper.AddressMapper;
import cn.zwx.member.svc.service.AddressService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author zhangwenuxe
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressMapper addressMapper;


    @Override
    public AddressEntity selectById(Long id) {
        return addressMapper.selectById(id);
    }

    @Override
    public List<AddressEntity> selectByUserId(String userId) {
        LambdaQueryWrapper<AddressEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressEntity::getUserId,userId);
        queryWrapper.orderByDesc(AddressEntity::getIsDefault);
        queryWrapper.orderByDesc(AddressEntity::getCreateTime);
        queryWrapper.orderByDesc(AddressEntity::getUpdateTime);
        return addressMapper.selectList(queryWrapper);
    }

    @Override
    public Long insert(AddressEntity address) {
        addressMapper.insertSelective(address);
        return address.getId();
    }

    @Override
    public void modify(AddressEntity address) {
        addressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public void delete(Long id) {
        addressMapper.deleteById(id);
    }

    @Override
    public boolean checkUserAddressExists(CheckUserAddressExistBO bo) {
        LambdaQueryWrapper<AddressEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressEntity::getUserId,bo.getUserId());
        queryWrapper.eq(AddressEntity::getProvinceId,bo.getProvinceId());
        queryWrapper.eq(AddressEntity::getCityId,bo.getCityId());
        queryWrapper.eq(AddressEntity::getDistrictId,bo.getDistrictId());
        queryWrapper.eq(AddressEntity::getAddress,bo.getAddress());
        final Long selectCount = addressMapper.selectCount(queryWrapper);
        return selectCount.compareTo(MemberConstans.LONG_0) > 0;
    }

    @Override
    public void cancelUserDefaultAddress(String userId) {
        LambdaQueryWrapper<AddressEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressEntity::getIsDefault, AddressTyepEnum.DEFAULT_.getCode());
        queryWrapper.eq(AddressEntity::getUserId, userId);
        final AddressEntity addressEntity = new AddressEntity();
        addressEntity.setIsDefault(AddressTyepEnum.DEFAULT_.getCode());
        addressEntity.setUpdateTime(new Date());
        addressMapper.update(addressEntity,queryWrapper);
    }

    @Override
    public AddressEntity selectDefaultUserAddress(String userId) {
        final LambdaQueryWrapperX<AddressEntity> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eq(AddressEntity::getUserId,userId);
        queryWrapperX.eq(AddressEntity::getIsDefault,AddressTyepEnum.DEFAULT_.getCode());
        return addressMapper.selectOne(AddressEntity::getIsDefault,AddressTyepEnum.DEFAULT_.getCode());
    }
}
